[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Caltech.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Caltech.App_Start.NinjectWebCommon), "Stop")]

namespace Caltech.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using CaltechSoft.Services;
    using CaltechSoft.Service.EntityFW.Entity;
    using CaltechSoft.Service.EntityFW.Context;
    using CaltechSoft.Service.Repositories;
    using CaltechSoft.Service.CommandService;

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);

            //Caltech.Bootstrapper.Initialise();
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);

                ServiceComponentsRegister.RegisterServicesComponents(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<UserManager<User>>().ToMethod(context =>
            {
                var userManager = new UserManager<User>(new UserStore<User>(new CaltechContext()));
                userManager.UserValidator = new UserValidator<User>(userManager) { AllowOnlyAlphanumericUserNames = false};

                return userManager;
            }).InRequestScope();
            kernel.Bind<RoleManager<Role>>().ToMethod(context => new RoleManager<Role>(new RoleStore<Role>(new CaltechContext()))).InRequestScope();
            kernel.Bind<IRepository<Quote>>().To<QuoteRepository>();
            kernel.Bind<IRepository<Po>>().To<PoRepository>();
            kernel.Bind<IRepository<Customer>>().To<CustomerCommandService>();
            kernel.Bind<IRepository<Equipment>>().To<EquipmentCommandService>();
            kernel.Bind<IRepository<CustomerEquipment>>().To<CustomerEquipmentCommandService>();
        }
    }
}
